package com.ktjy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktjy.entity.SysRight;
import com.ktjy.entity.SysRoleRight;
import com.ktjy.mapper.SysRightMapper;
import com.ktjy.mapper.SysRoleRightMapper;
import com.ktjy.service.ISysRoleRightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liuqiaomiao
 * @since 2024-03-01
 */
@Service
public class SysRoleRightServiceImpl extends ServiceImpl<SysRoleRightMapper, SysRoleRight> implements ISysRoleRightService {

    @Autowired
    SysRightMapper sysRightMapper;

    @Override
    public List<String> getRightByRoleId(Long roleId) {

        //第一步根据角色ID查询角色关联的资源ID集合
        QueryWrapper<SysRoleRight> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleRight::getRfRoleId, roleId);
        List<SysRoleRight> tempList = list(queryWrapper);


        //通过资源编号获取资料列表
        List<String> ids = new ArrayList<>();
        for (SysRoleRight temp : tempList) {
            ids.add(temp.getRfRightCode());
        }


        QueryWrapper<SysRight> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().in(SysRight::getRightCode, ids.toArray());
        List<SysRight> resutlList = sysRightMapper.selectList(queryWrapper1);
        ids = new ArrayList<>();
        for (SysRight temp : resutlList) {
            if (StringUtils.isNotEmpty(temp.getRightUrl().trim())) {
                ids.add(temp.getRightCode());
            }
        }
        return ids;
    }

    @Override
    public List<SysRight> getRightByUserRoleId(Long roleId) {

        //第一步根据角色ID查询角色关联的资源ID集合
        QueryWrapper<SysRoleRight> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleRight::getRfRoleId, roleId);
        List<SysRoleRight> tempList = list(queryWrapper);


        //通过资源编号获取资料列表
        List<String> ids = new ArrayList<>();
        for (SysRoleRight temp : tempList) {
            ids.add(temp.getRfRightCode());
        }


        QueryWrapper<SysRight> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().in(SysRight::getRightCode, ids.toArray());
        List<SysRight> resutlList = sysRightMapper.selectList(queryWrapper1);

        return resutlList;
    }
}
